/**
 * 
 */
package com.sci.web.action.datareportseason.innovatebase;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.sci.web.action.util.BaseAction;
import com.sci.web.bean.Enterprise_Funds;
import com.sci.web.dao.DataReportM_InnovateBaseDao;

/**
 * @author BaiFuyou
 * @date: Nov 7, 2011 3:53:03 PM
 * 
 * 创新活动基本情况 研发经费投入强度
 */
public class AddInnovateBaseData_ResearchMoneyAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8393415816529276339L;
	@Resource
	private DataReportM_InnovateBaseDao dataReportM_InnovateBaseDao;
	private int uid;
	private String f_sale;
	private String f_tech;
	private String f_invest;
	private String f_research;
	private String s_sale;
	private String s_tech;
	private String s_invest;
	private String s_research;
	private String t_sale;
	private String t_tech;
	private String t_invest;
	private String t_research;
	private String year;
	private int type;
	private int funds_intensity;

	/**
	 * @return the funds_intensity
	 */
	public int getFunds_intensity() {
		return funds_intensity;
	}

	/**
	 * @param funds_intensity
	 *            the funds_intensity to set
	 */
	public void setFunds_intensity(int funds_intensity) {
		this.funds_intensity = funds_intensity;
	}

	/**
	 * @return the f_sale
	 */
	public String getF_sale() {
		return f_sale;
	}

	/**
	 * @param f_sale
	 *            the f_sale to set
	 */
	public void setF_sale(String f_sale) {
		this.f_sale = f_sale;
	}

	/**
	 * @return the f_tech
	 */
	public String getF_tech() {
		return f_tech;
	}

	/**
	 * @param f_tech
	 *            the f_tech to set
	 */
	public void setF_tech(String f_tech) {
		this.f_tech = f_tech;
	}

	/**
	 * @return the f_invest
	 */
	public String getF_invest() {
		return f_invest;
	}

	/**
	 * @param f_invest
	 *            the f_invest to set
	 */
	public void setF_invest(String f_invest) {
		this.f_invest = f_invest;
	}

	/**
	 * @return the f_research
	 */
	public String getF_research() {
		return f_research;
	}

	/**
	 * @param f_research
	 *            the f_research to set
	 */
	public void setF_research(String f_research) {
		this.f_research = f_research;
	}

	/**
	 * @return the s_sale
	 */
	public String getS_sale() {
		return s_sale;
	}

	/**
	 * @param s_sale
	 *            the s_sale to set
	 */
	public void setS_sale(String s_sale) {
		this.s_sale = s_sale;
	}

	/**
	 * @return the s_tech
	 */
	public String getS_tech() {
		return s_tech;
	}

	/**
	 * @param s_tech
	 *            the s_tech to set
	 */
	public void setS_tech(String s_tech) {
		this.s_tech = s_tech;
	}

	/**
	 * @return the s_invest
	 */
	public String getS_invest() {
		return s_invest;
	}

	/**
	 * @param s_invest
	 *            the s_invest to set
	 */
	public void setS_invest(String s_invest) {
		this.s_invest = s_invest;
	}

	/**
	 * @return the s_research
	 */
	public String getS_research() {
		return s_research;
	}

	/**
	 * @param s_research
	 *            the s_research to set
	 */
	public void setS_research(String s_research) {
		this.s_research = s_research;
	}

	/**
	 * @return the t_sale
	 */
	public String getT_sale() {
		return t_sale;
	}

	/**
	 * @param t_sale
	 *            the t_sale to set
	 */
	public void setT_sale(String t_sale) {
		this.t_sale = t_sale;
	}

	/**
	 * @return the t_tech
	 */
	public String getT_tech() {
		return t_tech;
	}

	/**
	 * @param t_tech
	 *            the t_tech to set
	 */
	public void setT_tech(String t_tech) {
		this.t_tech = t_tech;
	}

	/**
	 * @return the t_invest
	 */
	public String getT_invest() {
		return t_invest;
	}

	/**
	 * @param t_invest
	 *            the t_invest to set
	 */
	public void setT_invest(String t_invest) {
		this.t_invest = t_invest;
	}

	/**
	 * @return the t_research
	 */
	public String getT_research() {
		return t_research;
	}

	/**
	 * @param t_research
	 *            the t_research to set
	 */
	public void setT_research(String t_research) {
		this.t_research = t_research;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		super.setContentType(super.JSON);
		int eid = dataReportM_InnovateBaseDao.getEnterpriseBaseId(uid);
		List<Enterprise_Funds> list = new ArrayList<Enterprise_Funds>();
		Enterprise_Funds eff = getFundsByStringtoFunds(eid, type, year, f_sale, f_tech, f_invest, f_research, funds_intensity);
		Enterprise_Funds efs = getFundsByStringtoFunds(eid, type, year, s_sale, s_tech, s_invest, s_research, funds_intensity);
		Enterprise_Funds eft = getFundsByStringtoFunds(eid, type, year, t_sale, t_tech, t_invest, t_research, funds_intensity);
		list.add(eff);
		list.add(efs);
		list.add(eft);
		boolean success = dataReportM_InnovateBaseDao.insertBatchEnterprise_Funds(list);
		if (success) {
			super.outPrint("ok");
		} else {
			super.outPrint("fail");
		}
		return "json";
	}

	/**
	 * @param eid
	 *            企业id
	 * @param type
	 *            类型 半年报
	 * @param year
	 *            申报的那一年
	 * @param f_sale
	 * @param f_tech
	 * @param f_invest
	 * @param f_research
	 * @param funds_intensity
	 * @return
	 */
	public Enterprise_Funds getFundsByStringtoFunds(int eid, int type, String year, String f_sale, String f_tech, String f_invest, String f_research,
			int funds_intensity) {

		String y = f_sale.substring(0, f_sale.indexOf("_"));
		double sale = Double.parseDouble(f_sale.substring(f_sale.indexOf("_") + 1, f_sale.length()));
		double tech = Double.parseDouble(f_tech.substring(f_tech.indexOf("_") + 1, f_tech.length()));
		double invest = Double.parseDouble(f_invest.substring(f_invest.indexOf("_") + 1, f_invest.length()));
		double research = Double.parseDouble(f_research.substring(f_research.indexOf("_") + 1, f_research.length()));
		double ratio = research / sale;
		Enterprise_Funds ef = new Enterprise_Funds();
		ef.setEditable(1);
		ef.setEnterprise_id(eid);
		ef.setFunds_base_investment(invest);
		ef.setFunds_check_pass(3);
		ef.setFunds_id(0);
		ef.setFunds_income(sale);
		ef.setFunds_intensity(funds_intensity);
		ef.setFunds_is_new(0);
		ef.setFunds_ratio(ratio*100);
		ef.setFunds_research_investment(research);
		ef.setFunds_technology_investment(tech);
		ef.setFunds_type(type);
		ef.setFunds_year(y);
		ef.setWhichyear(year);

		return ef;
	}

	/**
	 * @return the uid
	 */
	public int getUid() {
		return uid;
	}

	/**
	 * @param uid
	 *            the uid to set
	 */
	public void setUid(int uid) {
		this.uid = uid;
	}

	/**
	 * @return the year
	 */
	public String getYear() {
		return year;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(String year) {
		this.year = year;
	}

	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

}
